import { ApiProperty } from '@nestjs/swagger'
import { Exclude } from 'class-transformer'
import {
  Column,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
  OneToMany,
  Relation,
} from 'typeorm'

import { CompleteEntity } from '~/common/entity/common.entity'

import { AccessTokenEntity } from '~/modules/system/auth/entities/access-token.entity'

import { DeptEntity } from '~/modules/system/dept/dept.entity'
import { PostEntity } from '~/modules/system/post/post.entity'
import { RoleEntity } from '~/modules/system/role/role.entity'

@Entity({ name: 'system_user' })
export class UserEntity extends CompleteEntity {
  @Column({ type: 'varchar', length: 30, unique: true, comment: '用户账号' })
  @ApiProperty({ description: '用户账号' })
  username: string

  @Exclude()
  @Column({ type: 'varchar', length: 100, default: '', comment: '密码' })
  @ApiProperty({ description: '密码' })
  password: string

  @Column({ type: 'varchar', length: 30, comment: '用户昵称' })
  @ApiProperty({ description: '用户昵称' })
  nickname: string

  @Column({ type: 'varchar', length: 500, nullable: true, comment: '备注' })
  @ApiProperty({ description: '备注' })
  remark: string

  @Column({ type: 'bigint', nullable: true, name: 'dept_id', comment: '部门ID' })
  @ApiProperty({ description: '部门ID' })
  deptId: number

  @Column({ name: 'post_ids', type: 'json', nullable: true, comment: '岗位编号数组' })
  @ApiProperty({ description: '岗位编号数组' })
  postIds: number[]

  @Column({ type: 'varchar', length: 50, default: '', comment: '用户邮箱' })
  @ApiProperty({ description: '用户邮箱' })
  email: string

  @Column({ type: 'varchar', length: 11, default: '', comment: '手机号码' })
  @ApiProperty({ description: '手机号码' })
  mobile: string

  @Column({ type: 'tinyint', default: 0, comment: '用户性别' })
  @ApiProperty({ description: '用户性别' })
  sex: number

  @Column({ type: 'varchar', length: 512, default: '', comment: '头像地址' })
  @ApiProperty({ description: '头像地址' })
  avatar: string

  @Column({ type: 'tinyint', default: 0, comment: '帐号状态（0正常 1停用）' })
  @ApiProperty({ description: '帐号状态（0正常 1停用）' })
  status: number

  @Column({ name: 'login_ip', type: 'varchar', length: 50, default: '', comment: '最后登录IP' })
  @ApiProperty({ description: '最后登录IP' })
  loginIp: string

  @Column({ name: 'login_date', type: 'datetime', nullable: true, comment: '最后登录时间' })
  @ApiProperty({ description: '最后登录时间' })
  loginDate: Date

  @ManyToMany(() => RoleEntity, role => role.users)
  @JoinTable({
    name: 'system_user_roles',
    joinColumn: { name: 'user_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'role_id', referencedColumnName: 'id' },
  })
  roles: Relation<RoleEntity[]>

  @ManyToMany(() => PostEntity, post => post.users)
  @JoinTable({
    name: 'system_user_post',
    joinColumn: { name: 'user_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'post_id', referencedColumnName: 'id' },
  })
  posts: Relation<PostEntity[]>

  @ManyToOne(() => DeptEntity, dept => dept.users)
  @JoinColumn({ name: 'dept_id' })
  dept: Relation<DeptEntity>

  @OneToMany(() => AccessTokenEntity, accessToken => accessToken.user, {
    cascade: true,
  })
  accessTokens: Relation<AccessTokenEntity[]>
}
